<template>
  <div class="question-container">
    <div class="author-info">
      <img src="../../assets/images/avatar.jpg" alt="" class="photo" />
      <span class="message">~试试帮他解答</span>
      <Icon type="md-close" class="delete" @click="deleteItem"/>
    </div>
    <div class="question-info">
      <div class="question-title" @click="toDetail">
        <h1>{{ questionInfo.title }}</h1>z
      </div>
      <div class="bottom">
        <button class="write-answer">
          <Icon type="md-create" />
          <span @click="toAnswer">写回答</span>
        </button>
        <span class="answer-number">{{ questionInfo.answers }}回答</span>
        <span class="follow-number">{{ questionInfo.follows }}关注</span>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "@/util/axios";

export default {
  name: "QuestionItem",
  data() {
    return {};
  },
  props: {
    questionInfo: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  methods: {
    //点击问题去详情页
    toDetail() {
      //将该问题的信息记录下来
      this.$store.commit("changeQuestionInfo", this.questionInfo);

      let routeData = this.$router.resolve({
        path: `/question/${this.questionInfo.id}`,
      });
      window.open(routeData.href, "_blank");
    },
    //点击写回答去详情页写回答
    toAnswer() {
      this.$router.push({
        name: "question",
      });
      this.$store.commit("changeWriteAnswer", true);
    },
    //通知父组件删除该问题
    deleteItem() {
      this.$emit('deleteItem')
    }
  },
};
</script>

<style scoped lang="scss">
.question-container {
  width: 100%;
  padding: 16px 20px 16px 0;
  // height: 13.5rem;
  border-bottom: 1px solid #f6f6f6;
  .author-info {
    position: relative;
    display: flex;
    height: 2.5rem;
    line-height: 2.5rem;
    align-items: center;
    .photo {
      top: 0.25rem;
      width: 2rem;
      height: 2rem;
      border-radius: 50%;
    }
    .message {
      font-size: 1.5rem;
      color: rgb(133, 144, 166);
      margin-left: 0.5rem;
    }
    .delete {
      position: absolute;
      right: 1rem;
      cursor: pointer;
      display: none;
    }
  }
  .question-info {
    .question-title {
      margin-top: 1rem;
      width: 100%;
      h1 {
        color: #121212;
        cursor: pointer;
        font-size: 1.8rem;
        font-weight: 600;
        line-height: 1.6;
        &:hover {
          color: #175199;
        }
      }
    }
  }
  .bottom {
    margin-top: 2rem;
    .write-answer {
      color: #fff;
      width: 9.4rem;
      height: 2.6rem;
      background-color: #0084ff;
      padding: 1px solid #0084ff;
    }
    .answer-number {
      margin-left: 1.5rem;
      color: #8590a6;
    }
    .follow-number {
      color: #8590a6;
      &::before {
        content: "\B7";
        margin: 0 0.5rem;
      }
    }
  }
  &:hover .delete {
    display: block;
  }
}
</style>